package com.topcoder.srm488;

/**
 * John and Brus are bored. They have n+m common friends. n of them are bored 
 * and m are not. Every hour John and Brus randomly choose two different 
 * friends A and B. If there are several possible pairs (A, B), each one has 
 * the same probability of being chosen. After that, John has a talk with 
 * friend A and Brus has a talk with friend B. For each of two chosen friends, 
 * if the friend is not bored, he becomes bored after the talk.
 * 
 * You have to find the expected time for all the friends to become bored. 
 */
public class TheBordemDivOne {
	public double find(int n, int m) {
		double[] E = new double[m+1];
		E[0] = 0.0;
		int t = n + m;
		
		for (int i=1; i<=m; i++) {
			int C = t * (t - 1);
			int p1 = (t - i) * (t - i - 1);
			int p2 = 2 * i * (t - i);
			int p3 = i * (i - 1);
			
			E[i] = ( p2 * E[i-1] + p3 * (i>1? E[i-2] : 0.0) + C ) / (C - p1);
		}
		
		return E[m];
	}
}
